/* Ugame jquery function */
$.fn.flowUp = function(options) {
    var step = options["step"];
    var duration = options["duration"];
    var callback = options["callback"];

    $(this).animate({
            "position":"absolute",
            "top": "-=" + $(this).height() * step + "px"
        },
        duration,
        callback
    );
}
$.fn.flowLeft = function(options) {
    var step = options["step"];
    var duration = options["duration"];
    var callback = options["callback"];

    $(this).animate({
            "position":"absolute",
            "right": "+=" + $(this).width() * step + "px"
        },
        duration,
        callback
    );
}
$.fn.flowDown = function(options) {
    var step = options["step"];
    var duration = options["duration"];
    var callback = options["callback"];

    $(this).animate({
            "position":"absolute",
            "top": "+=" + $(this).height() * step + "px"
        },
        duration,
        callback
    );
}
$.fn.flowRight = function(options) {
    var step = options["step"];
    var duration = options["duration"];
    var callback = options["callback"];

    $(this).animate({
            "position":"absolute",
            "right": "-=" + $(this).width() * step + "px"
        },
        duration,
        callback
    );
}
/* ACTION PANEL */
$.fn.processScrollEventOnActionPanel = function() {
    if (!$(this).is(':hidden')) {
        $(this).hide();
        $(this).flowUp({
            step:"1",
            duration:"normal"
        });
    }
}
$.fn.addActionClickHandlerOnActionPanel = function(options) {
    var panel = options["panel"];
    var panels = options["disablePanels"];
    //init position for panel
    if (panel["animate"] == "LeftRight") {
        $(panel["id"]).flowRight({
            step:"1",
            duration:"normal"
        });
    } else {
        $(panel["id"]).flowUp({
            step:"1",
            duration:"normal"
        });
    }
    //register Scroll handler for window
    /*
     $(window).scroll(function(){
     $(panel).processScrollEventOnActionPanel();
     }).resize(function(){
     $(panel).processScrollEventOnActionPanel();
     });*/
    //register Click handler for button
    $(this).click(function() {
        //Hide all disable Panels
        jQuery.each(panels, function(i, panel) {
            if (!$(panel["id"]).is(':hidden')) {
                $(panel["id"]).hide();
                if (panel["animate"] == "LeftRight") {
                    $(panel["id"]).flowRight({
                        step:"1",
                        duration:"normal"
                    });
                } else {
                    $(panel["id"]).flowUp({
                        step:"1",
                        duration:"normal"
                    });
                }
            }
        });
        //Show or hide panel
        if (!$(panel["id"]).is(':hidden')) {
            if (panel["animate"] == "LeftRight") {
                $(panel["id"]).flowRight({
                    step:"1",
                    duration:"normal",
                    callback: function(){
                        $(panel["id"]).hide();
                    }
                });
            } else {
                $(panel["id"]).flowUp({
                    step:"1",
                    duration:"normal",
                    callback: function(){
                        $(panel["id"]).hide();
                    }
                });
            }
        }
        else {
            $(panel["id"]).show();
            if (panel["animate"] == "LeftRight") {
                $(panel["id"]).flowLeft({
                    step:"1",
                    duration:"normal"
                });
            } else {
                $(panel["id"]).flowDown({
                    step:"1",
                    duration:"normal"
                });
            }
        }
    });
}
$("#btn-login").addActionClickHandlerOnActionPanel({
    panel:{id:"#login-panel",animate:"LeftRight"},
    disablePanels: [
        {id:"#language-panel",animate:"UpDown"},
        {id:"#search-panel", animate:"LeftRight"}
    ]
});
$("#btn-search").addActionClickHandlerOnActionPanel({
    panel:{id:"#search-panel",animate:"LeftRight"},
    disablePanels: [
        {id:"#language-panel",animate:"UpDown"},
        {id:"#login-panel", animate:"LeftRight"}
    ]
});
$("#btn-language").addActionClickHandlerOnActionPanel({
    panel:{id:"#language-panel",animate:"UpDown"},
    disablePanels: [
        {id:"#login-panel", animate:"LeftRight"},
        {id:"#search-panel", animate:"LeftRight"}
    ]
});
/* Categories Panel*/
function drawCategoryDock(options) {
    var mainNav = options["mainNav"];
    var controlBtn = options["controlBtn"];
    var subNav = options["subNav"];
    var config = options["config"];

    $(subNav).hide();
    $(controlBtn).parent().removeClass("active");

    var bottom = $("body").height() - ($(window).height() + $(window).scrollTop());

    if (bottom > config.showBottom) {
        $(mainNav).css({
            position: "fixed",
            bottom: "0px"
        });
    } else {
        $(mainNav).css({
            position: "absolute",
            bottom: config.currentBottom + "px"
        });
    }
}
$.fn.controlSubCategoryNav = function controlSubCategoryNav(options) {
    $(this).click(function() {
        var mainNav = options["mainNav"];
        var subNav = options["subNav"];
        var config = options["config"];

        var categoryItemWidth = $(this).width() - 1;

        var bottom = $("body").height() - ($(window).height() + $(window).scrollTop());
        if (bottom > 247) {
            $(subNav).css({
                width:categoryItemWidth + "px",
                position: "fixed",
                bottom: $(mainNav).height() + "px"
            });
        } else {
            $(subNav).css({
                width:categoryItemWidth + "px",
                position: "absolute",
                bottom: $(mainNav).height() + config.currentBottom + "px"
            });
        }
        if ($(subNav).is(':hidden')) {
            $(this).parent().addClass("active");
        } else {
            $(this).parent().removeClass("active");
        }
        $(subNav).slideToggle("normal");
    });
}